package scau.erps.view.dashboard;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import org.jfree.chart.ChartPanel;


/**
 * 企业发展仪表盘
 * @author DCJ
 *
 */
public class DevelopmentPanel  extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	private JComboBox<String> itemCombo;
	private JButton selectBtn;
	private JPanel contentPanel,actionPanel,chartPanel;
	private JTextArea infoTextArea;
	public DevelopmentPanel(){				
		contentPanel = new JPanel();
  		contentPanel.setLayout(new BorderLayout());
		contentPanel.add(getActionPanel(),BorderLayout.NORTH);
		contentPanel.add(getChartPanel(),BorderLayout.CENTER);
		contentPanel.add(getInfoTextArea(),BorderLayout.SOUTH);
		this.add(contentPanel);
	}
	/**
	 * 切换不同统计指标Panel
	 * @return
	 */
	private JPanel getActionPanel(){
		String[] items = new String[]{"统计指标","销售额","净利润","总资产","负债","所有者权益","发展潜力","综合分数"};	
		itemCombo = new JComboBox<String>();
		for (int i = 0; i < items.length; i++) {
			itemCombo.addItem(items[i]);
		}
		selectBtn = new JButton("选择");		
		selectBtn.addActionListener(this);		
		actionPanel = new JPanel(); 
		actionPanel.add(itemCombo);		
		actionPanel.add(selectBtn);	
		return actionPanel;		
	}
	
	/**
	 * 图表Panel
	 * @return
	 */
	private JPanel getChartPanel(){		
		chartPanel = new JPanel(); 
		chartPanel.add(new ChartPanel(SaleChart.getChart()));	
		return chartPanel;
	}
	private void refreshChartPanel(ChartPanel chart){
		chartPanel.removeAll();
		chartPanel.add(chart);
		chartPanel.updateUI();
	}
	private JTextArea getInfoTextArea(){
		infoTextArea = new JTextArea();
		infoTextArea.setBorder(BorderFactory.createTitledBorder("说明"));
		infoTextArea.setText("显示企业重要的运营指标，展示企业发展情况");
		return infoTextArea;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==selectBtn){
			String item = null;
			if (itemCombo.getSelectedIndex() != 0) {
				item = (String) itemCombo.getSelectedItem();
			}
			if("销售额".equals(item)){
				this.refreshChartPanel(new ChartPanel(SaleChart.getChart()));
				infoTextArea.setText("销售额指对应年份所交货的订单金额总和；");
			}
			if("净利润".equals(item)){
				this.refreshChartPanel(new ChartPanel(ProfitChart.getChart()));
				infoTextArea.setText("净利润指对应年份公司所有收入扣除所有支出之后的金额；");
			}
            if("总资产".equals(item)){
            	this.refreshChartPanel(new ChartPanel(TotalAssetChart.getChart()));
            	infoTextArea.setText("总资产指对应年份公司现金、产成品等流动资产与厂房、生产线等固定资产总和；");
            }
            if("负债".equals(item)){
            	this.refreshChartPanel(new ChartPanel(TotalLoanChart.getChart()));
            	infoTextArea.setText("负债指对应年份长短期贷款、高利贷和应付款的总和；");
            }
            if("所有者权益".equals(item)){
            	this.refreshChartPanel(new ChartPanel(TotalEquityChart.getChart()));
            	infoTextArea.setText("所有者权益指对应年份股东资本与历年累计利润的总和;"+"\n所有者权益是判断公司盈利情况的重要指标；");
            }
            if("发展潜力".equals(item)){
            	this.refreshChartPanel(new ChartPanel(PotentialChart.getChart()));
            	infoTextArea.setText("发展潜力指对应年份公司生产能力、研发能力等指标分值总和;"+"\n发展潜力是判断公司实力以及未来发展的潜力的重要指标；");
            }
            if("综合分数".equals(item)){
            	this.refreshChartPanel(new ChartPanel(ScoreChart.getChart()));
            	infoTextArea.setText("综合分数指对应年份的综合实力，综合分数=所有者权益*(1+发展潜力/100);"+"\n综合分数是订单竞价匹配指标之一，也是公司最终成绩排名的依据；");
            }
		}				
	}
}
